package com.looookme.shaozhuo.mb_study;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import javax.security.auth.callback.Callback;

/**
 * Created by Shaozhuo on 2015/9/6.
 */
public class BasicAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private List<BasicData> mData;
    private Callback mCallBack;

    //自定义函数回掉接口
    public interface Callback{
        public void click(View v);
    }

    public BasicAdapter(Context mContext, List<BasicData> mData, Callback mCallBack) {
        this.mContext = mContext;
        this.mData = mData;
        this.mCallBack = mCallBack;
    }

    @Override
    public int getCount() {
        return this.mData.size();
    }

    @Override
    public Object getItem(int i) {
        return this.mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        BasicData obj = this.mData.get(i);
        ViewHolder viewHolder = null;
        if(view == null){
            viewHolder = new ViewHolder();

            view = LayoutInflater.from(this.mContext).inflate(R.layout.layout_list_item, null);

            viewHolder.textView = (TextView)view.findViewById(R.id.item_textView);
            viewHolder.imageView = (ImageView)view.findViewById(R.id.item_imageView);
            viewHolder.btn = (Button)view.findViewById(R.id.item_button);

            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)view.getTag();
        }

        if(obj != null){
            viewHolder.textView.setText(obj.getTextString());
            viewHolder.btn.setOnClickListener(this);
            viewHolder.btn.setTag(i);
        }
        return view;
    }

    @Override
    public void onClick(View view) {
        this.mCallBack.click(view);
    }

    static class ViewHolder{
        ImageView imageView;
        TextView textView;
        Button btn;
    }
}
